<?php
/**
 * AB-PHP DB Connection Facade for PDO
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Patrick J. Mizer
 * @package ab.core
 */
namespace Ab\Core;

class PdoDatabaseConnection Implements DatabaseConnection
{
    /**
     * @var PDO
     */
    private $_pdo;

    /**
     * Private constructor, access via getInstance singleton method
     * @return DatabaseConnection
     */
    public function __construct($dsn, $username, $password) {
        $this->_pdo = new \PDO($dsn, $username, $password);
        $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    }

    /**
     * Begin txn
     */
    public function beginTransaction() {
        $this->_pdo->beginTransaction();
    }

    /**
     * Commit txn
     */
    public function commitTransaction() {
        $this->_pdo->commit();
    }

    /**
     * Rollback txn
     */
    public function rollbackTransaction() {
        $this->_pdo->rollBack();
    }

    /**
     * Get last insert ID
     */
    public function getLastInsertId() {
        return $this->_pdo->lastInsertId();
    }

    /**
     * Prepare statement
     * 
     * @param String $sql
     * @return PDOStatement
     */
    public function prepare($sql) {
        return $this->_pdo->prepare($sql);
    }

    /**
     * Quote string
     *
     * @param String $str
     * @return String
     */
    public function quote($str) {
        return $this->_pdo->quote($str);
    }

}
?>
